using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;

namespace SeataNet.Core.Discovery.Nacos
{
    /// <summary>
    /// Nacos服务扩展
    /// </summary>
    public static class NacosServiceExtensions
    {
        /// <summary>
        /// 添加Nacos服务发现
        /// </summary>
        public static IServiceCollection AddNacosServiceDiscovery(
            this IServiceCollection services,
            Action<NacosConfiguration>? configureOptions = null)
        {
            // 配置Nacos
            if (configureOptions != null)
            {
                services.Configure(configureOptions);
            }

            // 注册HttpClient
            services.AddHttpClient("NacosClient")
                .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
                {
                    AutomaticDecompression = System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate,
                    UseProxy = false
                });

            // 注册Nacos服务发现
            services.TryAddSingleton<IServiceDiscovery, NacosServiceDiscovery>();

            return services;
        }

        /// <summary>
        /// 添加Nacos配置中心
        /// </summary>
        public static IServiceCollection AddNacosConfigService(
            this IServiceCollection services,
            Action<NacosConfiguration>? configureOptions = null)
        {
            // 配置Nacos
            if (configureOptions != null)
            {
                services.Configure(configureOptions);
            }

            // 注册HttpClient（如果未注册）
            if (!services.Any(s => s.ServiceType == typeof(IHttpClientFactory)))
            {
                services.AddHttpClient("NacosClient")
                    .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
                    {
                        AutomaticDecompression = System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate,
                        UseProxy = false
                    });
            }

            // 注册Nacos配置服务
            services.TryAddSingleton<INacosConfigService, NacosConfigService>();

            return services;
        }

        /// <summary>
        /// 添加完整的Nacos支持（服务发现 + 配置中心）
        /// </summary>
        public static IServiceCollection AddNacos(
            this IServiceCollection services,
            Action<NacosConfiguration>? configureOptions = null)
        {
            services.AddNacosServiceDiscovery(configureOptions);
            services.AddNacosConfigService(configureOptions);

            return services;
        }

        /// <summary>
        /// 使用Nacos服务发现替换默认实现
        /// </summary>
        public static IServiceCollection UseNacosServiceDiscovery(this IServiceCollection services)
        {
            // 移除默认服务发现
            var defaultDiscovery = services.FirstOrDefault(s => s.ServiceType == typeof(IServiceDiscovery));
            if (defaultDiscovery != null)
            {
                services.Remove(defaultDiscovery);
            }

            // 注册Nacos服务发现
            services.AddNacosServiceDiscovery();

            return services;
        }
    }
}
